package net.realsee.service.wcs.data.impl;

import cn.hutool.core.bean.BeanUtil;
import net.realsee.common.utils.StringUtils;
import net.realsee.domain.wcs.data.WcsStorageType;
import net.realsee.domain.wcs.data.bo.WcsStorageTypeBo;
import net.realsee.domain.wcs.data.vo.WcsStorageTypeVo;
import net.realsee.framework.core.page.TableDataInfo;
import net.realsee.framework.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import net.realsee.mapper.wcs.data.WcsStorageTypeMapper;
import net.realsee.service.wcs.data.IWcsStorageTypeService;
import org.springframework.stereotype.Service;


import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 存储区类型Service业务层处理
 *
 * @author realsee
 * @date 2023-08-21
 */
@RequiredArgsConstructor
@Service
public class WcsStorageTypeServiceImpl extends ServiceImpl<WcsStorageTypeMapper, WcsStorageType> implements IWcsStorageTypeService {

    private final WcsStorageTypeMapper baseMapper;

    /**
     * 查询存储区类型
     */
    @Override
    public WcsStorageTypeVo selectVoById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询存储区类型列表
     */
    @Override
    public TableDataInfo<WcsStorageTypeVo> selectVoPage(WcsStorageTypeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WcsStorageType> lqw = buildVoQueryWrapper(bo);
        Page<WcsStorageTypeVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询存储区类型列表
     */
    @Override
    public List<WcsStorageTypeVo> selectVoList(WcsStorageTypeBo bo) {
        LambdaQueryWrapper<WcsStorageType> lqw = buildVoQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WcsStorageType> buildVoQueryWrapper(WcsStorageTypeBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WcsStorageType> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getStorageType()), WcsStorageType::getStorageType, bo.getStorageType());
        lqw.eq(bo.getInvRemoval() != null, WcsStorageType::getInvRemoval, bo.getInvRemoval());
        lqw.eq(bo.getInvTransfer() != null, WcsStorageType::getInvTransfer, bo.getInvTransfer());
        lqw.like(StringUtils.isNotBlank(bo.getStorageName()), WcsStorageType::getStorageName, bo.getStorageName());
        return lqw;
    }

    /**
     * 新增存储区类型
     */
    @Override
    public Boolean insertByBo(WcsStorageTypeBo bo) {
        WcsStorageType add = BeanUtil.toBean(bo, WcsStorageType.class);
        return this.save(add);
    }

    /**
     * 修改存储区类型
     */
    @Override
    public Boolean updateByBo(WcsStorageTypeBo bo) {
        WcsStorageType update = BeanUtil.toBean(bo, WcsStorageType.class);
        return this.updateById(update);
    }

    /**
     * 批量删除存储区类型
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids) {
        return this.removeBatchByIds(ids);
    }
}
